import {checkBillId, COMMON, getCheckedRows} from "../../../main/util";
import CommonAction from "../../../actions/common";

export default class FormAction  extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    // 初始化
    didMount = () => {
        // 统一订阅事件
        this.subscribe();
    };

    // 卸载
    willUnMount = () => {
        this.pubSub.unSubscribe('showOrganization');
    };

    // 事件订阅部分
    subscribe = () => {
        const {pubSub} = this;
        pubSub.subscribe('showOrganization', (...args) => {
            this.showModel();
        });
    };
    // 切换编制维度
    changeOrgDimension = async (value) => {
        this.getOrganizationData(value);
    };

    /**
     * 获取编制数据
     * @returns {Promise<void>}
     */
    getOrganizationData = async (value)=>{
        const {props, state} = this.comp;
        const {dispatch, transfer} = props;
        let billids;
        if (transfer.page === COMMON.pageMain) {
            billids = getCheckedRows(props, 'pk_hi_stapply');
        }else if (transfer.page === COMMON.pageDetail) {
            billids = transfer.billid;
        }
        if(!checkBillId(this.toast,billids,transfer)) return;
        let postData = {
            billids,
            pk_org: transfer.hrorgobj.refpk,
            pk_dimension: value
        };
        let res = await dispatch({
            type: 'transfer/checkBudgetAction',
            payload: {
                postData: postData
            }
        });

        if(!res.success) {
            return;
        }
        const {org, dept} = res.data;
        let modelState = {
            //如果编制维度没有默认值 则设置为下拉列表的第一个的值
            orgStaOrgDimension: value || res.data.opts[0].value
        };
        //如果编制维度下拉列表为空则 设置值
        if(state.orgStaOptions.length<1){
            modelState.orgStaOptions = res.data.opts;
        }
        this.comp.setState(modelState);

        this.setOrgDeptTableData(org.org, dept.dept);
    };

    // 显示完了弹窗
    setOrgDeptTableData = (orgData, deptData) => {
        const {props} = this.comp;
        const {editTable} = props;
        let tableData = {rows:[]};
        !orgData && (orgData = tableData);
        !deptData && (deptData = tableData);

        editTable.setTableData('org', orgData, false);
        editTable.setTableData('dept', deptData, false);
    };

    // 切换tab的时候
    changeTab = (value) => {
        this.comp.setState({
            orgStaActiveTab: value
        });
    };

    showModel = async ()=>{
        await new Promise((resolve, reject) => {
            this.comp.setState({
                organizationVisible: true
            },resolve);
        });
        this.getOrganizationData()
    };

    /**
     * 关闭弹窗
     */
    closeBatchModal = async () => {
        this.comp.setState({
            organizationVisible: false
        });
        // await this.dispatch({
        //     type: 'transfer/update',
        //     payload: {
        //         organizationVisible: false
        //     }
        // });
    };
}